/*
 * Copyright (c) csii.com.cn 2016 zhaojin
 */

package com.csii.ssm.controller.tianhong;

import com.csii.ssm.common.Dict;
import com.csii.ssm.common.HeadFormatter;
import com.csii.ssm.controller.BaseController;
import com.csii.ssm.core.CommonDict;
import com.csii.ssm.dto.AjaxData;
import com.csii.ssm.exception.AccessRuntimeException;
import com.csii.ssm.exception.AccessRuntimeExceptionDict;
import com.csii.ssm.service.IPjnlService;
import com.csii.ssm.service.IUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.ServletContextAware;

import javax.inject.Inject;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 天虹支付/充值类控制器
 *
 * @author Charis
 * @create 2016-08-12-08:59
 */
@Controller
public class PaymentController extends ThBaseController implements ServletContextAware {

    private static final Logger logger = LoggerFactory.getLogger(PaymentController.class);

    @Inject
    private IUserService userService;

    @Inject
    private IPjnlService pjnlService;

    /**
     * 请求付款码
     * @param model
     * @param httpSession
     */
    @RequestMapping(value = Dict.TRS_GETUNLINEQRCODE, method = RequestMethod.GET)
    public void getUnlineQrCode(Model model, HttpSession httpSession) {
        try {
            //todo  	请求付款码
            // User user = (User) httpSession.getAttribute(Dict.LOGINUSER);
            //logger.debug(user.getTel());
            AjaxData ajaxData = getAjaxData();
            Map data = new HashMap();
            data.put("txn_id",headFormatter.getTxnMaping().get(Dict.TRS_GETUNLINEQRCODE));
            data.put("chnl","162");
            data.put("accno","162111");
            data.put("systrace","155123");
            data.put("pcode","123456");
            data.put("rc","155122");
            data.put("rc_detail","返回码解释:请求付款码");
            data.put("acq_id","012345");
            data.put("org_code","12345");
            data.put("teller","code123");
            data.put("txn_date","2016-8-30");
            data.put("txn_time",new Date());
            ajaxData.setDataMap(data);
            model.addAttribute(CommonDict.RES,ajaxData);
        } catch (AccessRuntimeException e) {
            throw new AccessRuntimeException(e.getMessage(), e);
        } catch (Exception e) {
            e.printStackTrace();
            throw new AccessRuntimeException(AccessRuntimeExceptionDict.ERROR_GETUNLINEQRCODE);
        }
    }
    /**
     * 响应订单消费
     * @param ent_mode 输入方式，取值为 00、 03 或 04
     * @param pcode ent_mode 为 03 或 04 （ 扫码支付的被扫方式）时上送，为付款码当 ent_mode 为 00 或 06 时，并且采用会员主扫支付方式时，为支付令牌
     * @param model
     * @param httpSession
     */
    @RequestMapping(value = Dict.TRS_ORDERCONSUME, method = RequestMethod.POST)
    public void orderConsume(@RequestParam(required=true) String resToken,@RequestParam String ent_mode ,@RequestParam String pcode,Model model, HttpSession httpSession) {
        try {
            //todo  	订单消费
            // User user = (User) httpSession.getAttribute(Dict.LOGINUSER);
            //logger.debug(user.getTel());
            AjaxData ajaxData = getAjaxData();
            Map data = new HashMap();
            data.put("txn_id",headFormatter.getTxnMaping().get(Dict.TRS_ORDERCONSUME));
            data.put("accno","20111");
            data.put("tr_amt","tr_amt");
            data.put("bal_amt","bal_amt");
            data.put("mid","mid");
            data.put("mch_name","mch_name");
            data.put("tid","tid");
            data.put("systrace","1234");
            data.put("voucher","12342");
            data.put("rrn","1245");
            data.put("rc","123");
            data.put("rc_detail","返回码解释:订单消费");
            data.put("acq_id","123");
            data.put("txn_date",new Date());
            data.put("txn_time",new Date());
            data.put("settle_date",new Date());
            data.put("memo","订单消费");
            ajaxData.setDataMap(data);
            model.addAttribute(CommonDict.RES,ajaxData);
        } catch (AccessRuntimeException e) {
            throw new AccessRuntimeException(e.getMessage(), e);
        } catch (Exception e) {
            e.printStackTrace();
            throw new AccessRuntimeException(AccessRuntimeExceptionDict.ERROR_ORDERCONSUME);
        }
    }

    /**
     * 响应支付预判
     * @param pcode ent_mode 为 03 或 04（扫码）时上送
     * @param model
     * @param httpSession
     */
    @RequestMapping(value = Dict.TRS_PAYPREDICT, method = RequestMethod.POST)
    public void payPredict(@RequestParam(required=true) String resToken,@RequestParam String pcode,Model model, HttpSession httpSession) {
        try {
            //todo  	支付预判
            // User user = (User) httpSession.getAttribute(Dict.LOGINUSER);
            //logger.debug(user.getTel());
            AjaxData ajaxData = getAjaxData();
            Map data = new HashMap();
            data.put("txn_id",headFormatter.getTxnMaping().get(Dict.TRS_PAYPREDICT));
            data.put("chnl","10000");
            data.put("accno","20111");
            data.put("systrace","12345");
            data.put("pin_tag","12312");
            data.put("sms_tag","0");
            data.put("rc","000");
            data.put("rc_detail","返回码解释:支付预判");
            data.put("acq_id","0001");
            data.put("org_code","9901");
            data.put("teller","1008611");
            data.put("txn_date",new Date());
            data.put("txn_time",new Date());
            ajaxData.setDataMap(data);
            model.addAttribute(CommonDict.RES,ajaxData);
        } catch (AccessRuntimeException e) {
            throw new AccessRuntimeException(e.getMessage(), e);
        } catch (Exception e) {
            e.printStackTrace();
            throw new AccessRuntimeException(AccessRuntimeExceptionDict.ERROR_PAYPREDICT);
        }
    }

    /**
     * 响应订单查询
     * @param model
     * @param httpSession
     */
    @RequestMapping(value = Dict.TRS_ORDERTRACKING, method = RequestMethod.GET)
    public void orderTracking(Model model, HttpSession httpSession) {
        try {
            //todo  	订单查询
            // User user = (User) httpSession.getAttribute(Dict.LOGINUSER);
            //logger.debug(user.getTel());
            AjaxData ajaxData = getAjaxData();
            Map data = new HashMap();
            data.put("txn_id",headFormatter.getTxnMaping().get( Dict.TRS_ORDERTRACKING));
            data.put("chnl","10000");
            data.put("voucher","2131");
            data.put("systrace","22121");
            data.put("acq_id","77890");
            data.put("org_code","00192910");
            data.put("teller","1243");
            data.put("txn_date",new Date());
            data.put("txn_time",new Date());
            data.put("rc","123");
            data.put("rc_detail","rc_detail:订单查询");
            data.put("tr_amt","121313");
            data.put("accno","12000");
            data.put("append","02");
            data.put("status","0000");
            ajaxData.setDataMap(data);
            model.addAttribute(CommonDict.RES,ajaxData);
        } catch (AccessRuntimeException e) {
            throw new AccessRuntimeException(e.getMessage(), e);
        } catch (Exception e) {
            e.printStackTrace();
            throw new AccessRuntimeException(AccessRuntimeExceptionDict.ERROR_ORDERTRACKING);
        }
    }

    /**
     * 响应支付订单撤销
     * @param model
     * @param httpSession
     */
    @RequestMapping(value = Dict.TRS_PAYORDERREVOKE, method = RequestMethod.GET)
    public void payOrderRevoke(Model model, HttpSession httpSession) {
        try {
            //todo  	支付订单撤销
            // User user = (User) httpSession.getAttribute(Dict.LOGINUSER);
            //logger.debug(user.getTel());
            AjaxData ajaxData = getAjaxData();
            Map data = new HashMap();
            data.put("txn_id",headFormatter.getTxnMaping().get(Dict.TRS_PAYORDERREVOKE));
            data.put("accno","30000");
            data.put("tr_amt ","200");
            data.put("mid ","123133");
            data.put("voucher","1234");
            data.put("systrace","22121");
            data.put("rrn ","77890");
            data.put("rc","123");
            data.put("rc_detail","rc_detail:订单查询");
            data.put("acq_id ","00192910");
            data.put("txn_date",new Date());
            data.put("txn_time",new Date());
            data.put("settle_date ",new Date());
            data.put("memo ","支付订单撤销");
            ajaxData.setDataMap(data);
            model.addAttribute(CommonDict.RES,ajaxData);
        } catch (AccessRuntimeException e) {
            throw new AccessRuntimeException(e.getMessage(), e);
        } catch (Exception e) {
            e.printStackTrace();
            throw new AccessRuntimeException(AccessRuntimeExceptionDict.ERROR_PAYORDERREVOKE);
        }
    }

    /**
     * x响应虹包账户预充值
     * @param ent_mode 输入方式，取值为 00、 03 或 04
     * @param pcode ent_mode 为 03 或 04（扫码）时上送
     * @param tr_amt 充值金额
     * @param model
     * @param httpSession
     */
    @RequestMapping(value = Dict.TRS_RECHARGECALLBACK4ALIPAYSERVERCONFIRM, method = RequestMethod.POST)
    public void rechargeCallBack4AlipayServerConfirm(@RequestParam(required=true) String resToken,@RequestParam String ent_mode,@RequestParam String pcode ,@RequestParam String tr_amt , Model model, HttpSession httpSession) {
        try {
            //todo  	虹包账户预充值
            // User user = (User) httpSession.getAttribute(Dict.LOGINUSER);
            //logger.debug(user.getTel());
            AjaxData ajaxData = getAjaxData();
            Map data = new HashMap();
            data.put("txn_id",headFormatter.getTxnMaping().get(Dict.TRS_RECHARGECALLBACK4ALIPAYSERVERCONFIRM));
            data.put("accno","20000");
            data.put("tr_amt ","200");
            data.put("bal_amt","12121");
            data.put("mid ","123133");
            data.put("mch_name","321dawd");
            data.put("tid","wqe32423");
            data.put("systrace","3243");
            data.put("voucher","2121");
            data.put("rrn","2212");
            data.put("rc","134");
            data.put("rc_detail","返回码解释:虹包账户预充值");
            data.put("acq_id","1234");
            data.put("txn_date",new Date());
            data.put("txn_time",new Date());
            data.put("settle_date",new Date());
            data.put("memo","虹包账户预充值");
            ajaxData.setDataMap(data);
            model.addAttribute(CommonDict.RES,ajaxData);
        } catch (AccessRuntimeException e) {
            throw new AccessRuntimeException(e.getMessage(), e);
        } catch (Exception e) {
            e.printStackTrace();
            throw new AccessRuntimeException(AccessRuntimeExceptionDict.ERROR_RECHARGECALLBACK4ALIPAYSERVERCONFIRM);
        }
    }

    /**
     * 请求充值订单激活
     * @param model
     * @param httpSession
     */
    @RequestMapping(value = Dict.TRS_RECHARGEORDERACTIVATE, method = RequestMethod.GET)
    public void rechargeOrderActivate( Model model, HttpSession httpSession) {
        try {
            //todo  	充值订单激活
            // User user = (User) httpSession.getAttribute(Dict.LOGINUSER);
            //logger.debug(user.getTel());
            AjaxData ajaxData = getAjaxData();
            Map data = new HashMap();
            data.put("txn_id",headFormatter.getTxnMaping().get(Dict.TRS_RECHARGEORDERACTIVATE));
            data.put("tr_amt ","5200");
            data.put("systrace","3243");
            data.put("voucher","2121");;
            data.put("rc","134");
            data.put("rc_detail","返回码解释:充值订单激活");
            data.put("acq_id","1234");
            data.put("txn_date",new Date());
            data.put("txn_time",new Date());
            data.put("settle_date",new Date());
            data.put("memo","充值订单激活");
            ajaxData.setDataMap(data);
            model.addAttribute(CommonDict.RES,ajaxData);
        } catch (AccessRuntimeException e) {
            throw new AccessRuntimeException(e.getMessage(), e);
        } catch (Exception e) {
            e.printStackTrace();
            throw new AccessRuntimeException(AccessRuntimeExceptionDict.ERROR_RECHARGEORDERACTIVATE);
        }
    }

    /**
     * 充值订单撤销请求
     * @param model
     * @param httpSession
     */
    @RequestMapping(value = Dict.TRS_RECHARGEORDERREVOKE, method = RequestMethod.GET)
    public void rechargeOrderRevoke( Model model, HttpSession httpSession) {
        try {
            //todo  	充值订单撤销
            // User user = (User) httpSession.getAttribute(Dict.LOGINUSER);
            //logger.debug(user.getTel());
            AjaxData ajaxData = getAjaxData();
            Map data = new HashMap();
            data.put("txn_id",headFormatter.getTxnMaping().get(Dict.TRS_RECHARGEORDERREVOKE));
            data.put("tr_amt ","5210");
            data.put("systrace","3243");
            data.put("voucher","2121");;
            data.put("rc","134");
            data.put("rc_detail","返回码解释:充值订单撤销");
            data.put("acq_id","1234");
            data.put("txn_date",new Date());
            data.put("txn_time",new Date());
            data.put("settle_date",new Date());
            data.put("memo","充值订单撤销");
            ajaxData.setDataMap(data);
            model.addAttribute(CommonDict.RES,ajaxData);
        } catch (AccessRuntimeException e) {
            throw new AccessRuntimeException(e.getMessage(), e);
        } catch (Exception e) {
            e.printStackTrace();
            throw new AccessRuntimeException(AccessRuntimeExceptionDict.ERROR_RECHARGEORDERREVOKE);
        }
    }

    /**
     * 支付令牌请求
     * @param model
     * @param httpSession
     */
    @RequestMapping(value = Dict.TRS_PAYTOKENREQUEST, method = RequestMethod.GET)
    public void payTokenRequest( Model model, HttpSession httpSession) {
        try {
            //todo  	支付令牌请求
            // User user = (User) httpSession.getAttribute(Dict.LOGINUSER);
            //logger.debug(user.getTel());
            AjaxData ajaxData = getAjaxData();
            Map data = new HashMap();
            data.put("txn_id",headFormatter.getTxnMaping().get(Dict.TRS_PAYTOKENREQUEST));
            data.put("chnl ","1660");
            data.put("systrace","3243");
            data.put("voucher","2121");
            data.put("tr_amt ","1660");
            data.put("pcode","134");
            data.put("ttl","134");
            data.put("rc","134");
            data.put("rc_detail","返回码解释:支付令牌请求");
            data.put("acq_id","1234");
            data.put("org_code ","1234");
            data.put("teller ","1234");
            data.put("txn_date",new Date());
            data.put("txn_time",new Date());
            ajaxData.setDataMap(data);
            model.addAttribute(CommonDict.RES,ajaxData);
        } catch (AccessRuntimeException e) {
            throw new AccessRuntimeException(e.getMessage(), e);
        } catch (Exception e) {
            e.printStackTrace();
            throw new AccessRuntimeException(AccessRuntimeExceptionDict.ERROR_PAYTOKENREQUEST);
        }
    }

    @Override
    public void setServletContext(ServletContext servletContext) {
        this.servletContext = servletContext;
    }
}


